#!/usr/bin/python
# -*- coding:utf-8 -*-


def fun(a, b, c=10):
    return a + b + c


print(fun(1, 2, 3))

print(fun(1, c=3, b=2))

print(fun(3, 2))


def func(*name):
    print type(name)
    print name


func(1, 4, 6)
func(5, 6, 7, 1, 2, 3)


def func(**dict):
    print type(dict)
    print dict


func(a=1, b=9)
func(m=2, n=1, c=11)


def func(a, b, c):
    print a, b, c


args = (1, 3, 4)
func(*args)


def line_conf():
    b = 15

    def line(x):
        return 2 * x + b

    return line  # return a function object


b = 5
my_line = line_conf()
print(my_line(5))
print(b)


def line_conf(a, b):
    def line(x):
        return a * x + b

    return line


line1 = line_conf(1, 1)
line2 = line_conf(4, 5)
print(line1(5), line2(5))

"""
不带参数的装饰器
"""


def decorator(f):
    def new_f(a, b):
        print("input", a, b)
        return f(a, b)

    return new_f


# get square sum
@decorator
def square_sum(a, b):
    return a ** 2 + b ** 2


# get square diff
@decorator
def square_diff(a, b):
    return a ** 2 - b ** 2


print(square_sum(3, 4))
print(square_diff(3, 4))

"""
带参数的装饰器
"""


# a new wrapper layer
def pre_str(pre=''):
    # old decorator
    def decorator(F):
        def new_F(a, b):
            print(pre + "input", a, b)
            return F(a, b)

        return new_F

    return decorator


# get square sum
@pre_str('^_^')
def square_sum(a, b):
    return a ** 2 + b ** 2


# get square diff
@pre_str('T_T')
def square_diff(a, b):
    return a ** 2 - b ** 2


print(square_sum(3, 4))
print(square_diff(3, 4))


def decorator(aclass):
    class NewClass:
        def __init__(self, age):
            self.total_display = 0
            self.wrapped = aclass(age)

        def display(self):
            self.total_display += 1
            print("total display", self.total_display)
            self.wrapped.display()

    return NewClass


@decorator
class Bird:
    def __init__(self, age):
        self.age = age

    def display(self):
        print("My age is", self.age)


eagleLord = Bird(5)
for i in range(3):
    eagleLord.display()

print(id(eagleLord))

from sys import getrefcount

a = [1, 2, 3]
b = a
print(getrefcount(a))


class FromObj(object):
    def __init__(self, to_obj):
        self.to_obj = to_obj


b = [1, 2, 3]
a = FromObj(b)
print(id(a.to_obj))
print(id(b))

import gc

print(gc.get_threshold())

print('-'.join("this is a word"))

print(a.__class__.__name__)


